*REVISION for TAX AUTONOMY paper
*full period length 1984-2019

 use "D:\Tax_autonomy\data\taxautonomy_updated.dta", clear

*create variables/GDP and rename them
for var nom*: gen _X = X/pib
rename _nom* *

*create real variables/population (per capita at constant prices 2016) and rename them
for var nom*: gen Xcap = X*deflccaa/pop
rename nom*cap *cap

*convert pop into millions of people
replace pop=pop/1000000

*convert pop variables into per one share
replace pop65up=pop65up/100
replace pop15less=pop15less/100
replace employmentr=employmentr/100
replace unemploymentr=unemploymentr/100

xtset unit year

 *generate interaction variables
 gen histaxrev=histccaa*taxrevenues
 gen histcurropexp=histccaa*currentoperexp
 gen histcurrtrans=histccaa*currenttransfers
 gen histL1def=histccaa*L1.primarydef
 gen aftertaxrev=after*taxrevenues
 gen aftercurropexp=after*currentoperexp
 gen aftercurrtrans=after*currenttransfers
 gen afterL1def=after*L1.primarydef
 gen afterL1debt=after*L1.debt
 gen afterdebt=after*debt
 
 gen afterhistcurropexp=after*histcurropexp
 
 gen aftertaxrevcap=after*taxrevenuescap
 gen aftercurropexpcap=after*currentoperexpcap
 gen aftercurrtrancap=after*currenttransferscap

*generate real debt growth (December-to-December)
gen realdebt=nomdebt*deflccaa
bys unit: gen debtgrowth=(realdebt-realdebt[_n-1])/realdebt[_n-1]
	*debt change over GDP
bys unit: gen debtchange=(nomdebt-nomdebt[_n-1])/pib[_n-1]

*generate lagged or lead variables
bys unit: gen L1def=L1.primarydef
bys unit: gen L1defcap=L1.primarydefcap
bys unit: gen L1curropexp=L1.currentoperexp
bys unit: gen L1aftercurropexp=L1.aftercurropexp


*****************************************************************************
**TEST FOR STRUCTURAL BREAKS:
*CONTROLLING for cross-section dependence (with csa(L.primarydef)) and for We account for
*heteroskedasticity and autocorrelation by using an HAC robust variance estimator with the option vce(hac)

xtbreak test primarydef currentoperexp currenttransfers growth, h(2) breaks(1)  vce(hac) csa(L.primarydef)

*Whithout the previous controls for dependence, heterosked. and autocorrel:

xtbreak test primarydef currentoperexp currenttransfers growth, h(2) breaks(1)

***********************************
****REGRESSIONS WITH 17 REGIONS****
log using taxautonomy_17regions
****REGRESSIONS WITH 17 REGIONS****

	*Model [1]
xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(taxrevenues currentoperexp aftercurropexp histaxrev histcurropexp afterhistcurropexp) artests(2)
 estat sargan
 xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(taxrevenues currentoperexp aftercurropexp histaxrev histcurropexp afterhistcurropexp) artests(2) twostep
 estat abond

	*Model [2]
xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(taxrevenues currentoperexp aftercurropexp histaxrev histcurropexp afterhistcurropexp) artests(2)
 estat sargan
 xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(taxrevenues currentoperexp aftercurropexp histaxrev histcurropexp afterhistcurropexp) artests(2) twostep
 estat abond
	*Model [2b]
 xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(histL1def taxrevenues currentoperexp aftercurropexp histaxrev histcurropexp afterhistcurropexp) artests(2)
   estat sargan
   xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(histL1def taxrevenues currentoperexp aftercurropexp histaxrev histcurropexp afterhistcurropexp) artests(2) twostep
   estat abond
 
	*Model [3]
 xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2)
	 estat sargan
	xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2) twostep
	estat abond

	*Model [3b]
xtabond primarydef pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2)
	 estat sargan
	xtabond primarydef pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2) twostep
	estat abond

	*Model[4]
xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2)
	 estat sargan	
	xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2) twostep
	estat abond
	 
	 *Model [4b]
xtabond primarydef pop65up pop15less, lags(1) pre (growth) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2)
	estat sargan
	xtabond primarydef pop65up pop15less, lags(1) pre (growth) endog(currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2) twostep
	estat abond

	*Model [4c]
xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(histL1def currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2)
	estat sargan
	xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(histL1def currenttransfers currentoperexp aftercurropexp histcurrtrans histcurropexp afterhistcurropexp) artests(2) twostep
	estat abond
	
log close
translate taxautonomy_17regions.smcl taxautonomy_17regions.log

*******************************************************************
****REGRESSIONS WITHOUT BASQUE COUNTRY AND NAVARRE FROM NOW ON!****
*drop Basque Country and Navarre
drop if histccaa==1

log using taxautonomy_revision
***************************************************************************
 *REGRESSIONS with primarydef/GDP
	*main regressor: TAX REVENUES
	***NOTE: regressions with population turn taxrev not significant once you have entered piramidal pop variables.
	
*xtabond primarydef pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(aftertaxrev aftercurropexp) artests(2)
	*Model[1]
xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(taxrevenues currentoperexp aftercurropexp) artests(2)
	 estat sargan
	xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(taxrevenues currentoperexp aftercurropexp) artests(2) twostep
	 estat abond

	
*xtabond primarydef pop65up pop15less, lags(1) pre (growth) endog(aftertaxrev aftercurropexp) artests(2)
	*Model[2]
xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(taxrevenues currentoperexp aftercurropexp) artests(2)
	 estat sargan
	*xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(taxrevenues currentoperexp aftertaxrev aftercurropexp) artests(2) twostep
	xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(currentoperexp aftercurropexp) artests(2) twostep
	estat abond
	 
	 
	*main regressor: CURRENT TRANSFERS (income)
*xtabond primarydef pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(aftercurrtrans aftercurropexp) artests(2)
	*Model[3]
xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp aftercurropexp) artests(2)
	 estat sargan
	 xtabond primarydef pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp aftercurropexp) artests(2) twostep
	 estat abond
	 
*xtabond primarydef pop65up pop15less, lags(1) pre (growth) endog(aftercurrtrans aftercurropexp) artests(2)
	*Model[4]
xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(currenttransfers currentoperexp aftercurropexp) artests(2)
	 estat sargan
	 *xtabond primarydef pop65up pop15less, lags(1) pre (growth) endog(currenttransfers currentoperexp aftercurrtrans aftercurropexp) artests(2) twostep
	 xtabond primarydef pop65up pop15less after, lags(1) pre (growth) endog(currenttransfers currentoperexp aftercurropexp) artests(2) twostep
	 estat abond
		
		*try regression without Basque Country and Navarre
		*xtabond primarydef pop if (unit!=15 & unit!=16), lags(1) pre (employmentr growth agriculture after pop65up pop15less) endog(taxrevenues currentexp aftertaxrev aftercurrexp) artests(2)

		*try regression for Basque Country and Navarre only)
		*xtabond primarydef pop if(histccaa==1), lags(1) pre (employmentr growth agriculture after pop65up pop15less) endog(taxrevenues currentexp aftertaxrev aftercurrexp) artests(2)

******************************************************************************** aftercurropexpcap currentoperexpcap
*REGRESSIONS with primarydef and vars in per capita terms
	*main regressor: TAX REVENUES
*xtabond primarydefcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(aftercurropexpcap aftertaxrevcap) artests(2)
	*Model[1]per cap
xtabond primarydefcap pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(taxrevenuescap currentoperexpcap aftercurropexpcap) artests(2)
	 estat sargan
	*xtabond primarydefcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(taxrevenuescap currentoperexpcap aftercurropexpcap aftertaxrevcap) artests(2) twostep
	xtabond primarydefcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(taxrevenuescap currentoperexpcap) artests(2) twostep
	estat abond

	 
*xtabond primarydefcap pop65up pop15less, lags(1) pre (growth) endog(aftercurropexpcap aftertaxrevcap) artests(2)
	*Model[2]per cap
xtabond primarydefcap pop65up pop15less after, lags(1) pre (growth) endog(taxrevenuescap currentoperexpcap aftercurropexpcap) artests(2)
	 estat sargan
	*xtabond primarydefcap pop65up pop15less, lags(1) pre (growth) endog(taxrevenuescap currentoperexpcap aftercurropexpcap) artests(2) twostep
	xtabond primarydefcap pop65up pop15less, lags(1) pre (growth) endog(taxrevenuescap currentoperexpcap) artests(2) twostep
	estat abond
	
	*main regressor: CURRENT TRANSFERS (income)
*xtabond primarydefcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(aftercurrtrancap aftercurropexpcap) artests(2)
	*Model[3]per cap
xtabond primarydefcap pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(currenttransferscap currentoperexpcap aftercurropexpcap) artests(2)
	 estat sargan
	 *xtabond primarydefcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(currenttransferscap currentoperexpcap aftercurrtrancap aftercurropexpcap) artests(2) twostep
 	 xtabond primarydefcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(currenttransferscap currentoperexpcap) artests(2) twostep
	 estat abond
	 
*xtabond primarydefcap pop65up pop15less, lags(1) pre (growth) endog(aftercurrtrancap aftercurropexpcap) artests(2)
	*Model[4]per cap
xtabond primarydefcap pop65up pop15less after, lags(1) pre (growth) endog(currenttransferscap currentoperexpcap aftercurropexpcap) artests(2)
	  estat sargan
	  *xtabond primarydefcap pop65up pop15less, lags(1) pre (growth) endog(currenttransferscap currentoperexpcap aftercurrtrancap aftercurropexpcap) artests(2) twostep
	  xtabond primarydefcap pop65up pop15less, lags(1) pre (growth) endog(currenttransferscap currentoperexpcap) artests(2) twostep
	  estat abond

			*regression to compare with reg 1 in table 5 v1
			*xtabond primarydef pop if(year<2002 & unit!=15 & unit!=16), lags(1) pre (employmentr growth agriculture) endog(taxrevenues currentexp) artests(2)

**************************************************************************
**************************************************************************
*REGRESSIONS with December DEBT/GDP as dependent variable
	*main regressor: TAX REVENUES
*xtabond debt pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(aftertaxrev aftercurropexp L1def) artests(2)
	*Model[1] Debt
xtabond debt pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(taxrevenues currentoperexp L1def aftercurropexp) artests(2)
	 estat sargan
	xtabond debt pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(taxrevenues currentoperexp L1def aftercurropexp) artests(2) twostep
	 estat abond //bad results
	 
*xtabond debt pop65up pop15less, lags(1) pre(growth) endog(aftertaxrev aftercurropexp L1def) artests(2)
	*Model[2] Debt
xtabond debt pop65up pop15less after, lags(1) pre(growth) endog(taxrevenues currentoperexp L1def aftercurropexp) artests(2)
	 estat sargan
	*xtabond debt pop65up pop15less, lags(1) pre(growth) endog(taxrevenues currentoperexp L1def aftertaxrev aftercurropexp) artests(2) twostep
	 xtabond debt pop65up pop15less after, lags(1) pre(growth) endog(taxrevenues currentoperexp L1def aftercurropexp) artests(2) twostep
	 estat abond
	 
	*main regressor: CURRENT TRANSFERS (income)
*xtabond debt pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentexp L1def) artests(2)
*xtabond debt pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(L1def aftercurrtrans aftercurropexp) artests(2)
	*Model[3] Debt
xtabond debt pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp L1def aftercurropexp) artests(2)
	 estat sargan
	xtabond debt pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(currenttransfers currentoperexp L1def aftercurropexp) artests(2) twostep
	estat abond
	 
*xtabond debt pop65up pop15less, lags(1) pre(growth) endog(currenttransfers currentexp L1def) artests(2)
*xtabond debt pop65up pop15less, lags(1) pre(growth) endog(L1def aftercurrtrans aftercurropexp) artests(2)
	*Model[4] Debt
xtabond debt pop65up pop15less after, lags(1) pre(growth) endog(currenttransfers currentoperexp L1def aftercurropexp) artests(2)
	 estat sargan
	 xtabond debt pop65up pop15less , lags(1) pre(growth) endog(currenttransfers currentoperexp L1def aftercurrtrans) artests(2) twostep
	 estat abond
	 

************************************
*with December DEBT and vars in per capita terms
	*main regressor: TAX REVENUES
*xtabond debtcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(aftertaxrevcap aftercurropexpcap L1defcap) artests(2)
	*Model[1] Debt per cap
xtabond debtcap pop65up pop15less after, lags(1) pre (employmentr growth agriculture) endog(taxrevenuescap currentoperexpcap aftercurropexpcap) artests(2)
	 estat sargan
	 *xtabond debtcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(taxrevenuescap currentoperexpcap L1defcap aftertaxrevcap aftercurropexpcap) artests(2) twostep
	 xtabond debtcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(currentoperexpcap aftertaxrevcap) artests(2) twostep
	 estat abond
	 
*xtabond debtcap pop65up pop15less, lags(1) pre (growth employmentr) endog(aftertaxrevcap aftercurropexpcap L1defcap) artests(2)
	*Model[2] Debt per cap
xtabond debtcap pop65up pop15less after, lags(1) pre (growth employmentr) endog(taxrevenuescap currentoperexpcap aftercurropexpcap) artests(2)
	 estat sargan
	*xtabond debtcap pop65up pop15less, lags(1) pre (growth employmentr) endog(taxrevenuescap currentoperexpcap L1defcap aftertaxrevcap aftercurropexpcap) artests(2) twostep
	 xtabond debtcap pop65up pop15less, lags(1) pre (growth employmentr) endog(currentoperexpcap aftertaxrevcap) artests(2) twostep
	 estat abond
	 
	*main regressor: CURRENT TRANSFERS (income) -- BAD RESULTS
*xtabond debtcap pop65up pop15less, lags(1) pre (employmentr growth agriculture) endog(aftercurrtrancap aftercurropexpcap L1defcap) artests(2)
	*Model[3] Debt per cap
xtabond debtcap pop65up pop15less after, lags(1) pre (growth) endog(currenttransferscap currentoperexpcap L1defcap aftercurropexpcap) artests(2)
	 estat sargan
	*xtabond debtcap pop65up pop15less, lags(1) pre (growth) endog(currenttransferscap currentoperexpcap L1defcap aftercurrtrancap aftercurropexpcap) artests(2) twostep
	 xtabond debtcap pop65up pop15less, lags(1) pre (growth) endog(currenttransferscap currentoperexpcap L1defcap) artests(2) twostep
	 estat abond
	 
*xtabond debtcap pop65up , lags(1) pre (growth employmentr) endog(aftercurrtrancap aftercurropexpcap L1defcap) artests(2)
	*Model[4] Debt per cap
xtabond debtcap pop65up after, lags(1) pre (growth) endog(currenttransferscap currentoperexpcap L1defcap aftercurropexpcap) artests(2)
	 estat sargan
	 *xtabond debtcap pop65up , lags(1) pre (growth) endog(currenttransferscap currentoperexpcap L1defcap aftercurrtrancap aftercurropexpcap) artests(2) twostep
	 xtabond debtcap pop65up , lags(1) pre (growth) endog(currenttransferscap currentoperexpcap L1defcap) artests(2) twostep
	 estat abond

log close
translate taxautonomy_revision.smcl taxautonomy_revision.log	 
**************************************************************************
log using taxautonomy_revision_MMR
**************************************************************************
		*MULTIVARIATE MULTIPLE REGRESSIONS:
	 
***IS this a good technique for our study????***
*Steps: https://www.projectguru.in/application-of-multivariate-regression-analysis/
		
*3. test previous to MMReg application:
*ssc install robmv
	 robmv mcd primarydef currenttransfers //detect outliers in dep variables
*net install st0173_1 //for mcd command (outliers detections)

		*check last columns in dataset after running this below:
mcd primarydef currenttransfers employmentr growth agriculture taxrevenues currentoperexp pop65up pop15less aftercurropexp,  outlier

*4. Multivariate normality: Normality in each of the dependent variable states the presence of symmetricity in the table
		*DOES NOT PASS THESE TESTS: for some units, multivariate test/s reject/s normality at 5%.
	xtset unit year
by unit: mvtest norm primarydef currenttransfers, bivariate univariate stats(all) //p-value<0.05 rejection of normality at 5%.

*5. Variable reliability: efficiency of variables in measuring the respective construct. Dependent variables: their Cronbach alpha value should be more than 0.7 (Laerd, 2021).
	*DOES NOT PASS THIS TEST: alpha<.7
by unit: alpha primarydef currenttransfers

*7. Absence of multicollinearity (moderate corr should exist between dependent variables)
by unit: corr primarydef currenttransfers

*8. Homogeneity of the variance-covariance matrix (DOES NOT PASS THESE TESTS)
	*Box M test of equality of covariance (NO HOMOGENEITY)
mvtest cov primarydef currenttransfers employmentr growth agriculture taxrevenues currentoperexp pop65up pop15less, by(unit)
	*Levene's test of variance equality across groups (there IS a statistically significant DIFFERENCE in the variance of dep var among units)
robvar primarydef, by(unit)
robvar currenttransfers, by(unit)



bys unit: gen L1primarydef=L1.primarydef

manova primarydef currenttransfers = c.L1primarydef c.employmentr c.growth c.agriculture c.taxrevenues c.currentoperexp c.pop65up c.pop15less after c.aftercurropexp
		*p-value after Model=0.0000 IT IS REASONABLE to use both dependent variables simultaneously
*to estimate the model specified just above, after command manova, we can run:
mvreg,corr
		*to run a general multivariate model: mvreg y1 y2 y3 = x1 x2 i.catvar
mvreg primarydef currenttransfers = L1.primarydef employmentr growth agriculture taxrevenues currentoperexp pop65up pop15less after aftercurropexp, corr

*NOTE: mvreg,corr <- gives corr matrix between the 2 regressions and
*		the Breusch-Pagan test of independence of residuals: IT'S SIGNIFICANT==residuals NOT independent.

//////////////Wald tests of simple and composite linear hypotheses:////////////
*test jointly significance of coefficients:
test L1.primarydef employmentr growth agriculture taxrevenues currentoperexp pop65up pop15less after aftercurropexp
*test jointly significance of equations:
test [primarydef]
test [currenttransfers], accum

///// to interpret all the tests below, note the following:
	*In the full 2-dependent regression there are 10+10 regressors
	*Conservative Statistician: Hence, p-value> 0.05/20=0.0025 should be considered not significant at 5%
	*Agressive Statistician: Since the 2 eq are jointly significant, we may consider p-val> 0.05/10 as not significant at 5%
	*Source: Stata mvreg command manual.
	
*test if coeffs are zero in both regressions:
test employmentr //it's NOT ZERO!
test agriculture //it is NOT ZERO!
test pop65up //it's NOT zero

*joint test of significance for each regressor:
	*We test those with high p-values in 1 of the two equations.
	*they are all jointly significant
 test [primarydef]employmentr [currenttransfers]employmentr
 test [primarydef]taxrevenues [currenttransfers]taxrevenues
 test [primarydef]currentoperexp [currenttransfers]currentoperexp
 test [primarydef]pop65up [currenttransfers]pop65up
 test [primarydef]pop15less [currenttransfers]pop15less
  
*test if coeffs are identical for both regressions:
*test [primarydef=currenttransfers]: L1.primarydef
test [primarydef=currenttransfers]: growth
test [primarydef=currenttransfers]: agriculture
test [primarydef=currenttransfers]: taxrevenues
test [primarydef=currenttransfers]: currentoperexp
test [primarydef=currenttransfers]: pop65up
test [primarydef=currenttransfers]: aftercurropexp
test [primarydef=currenttransfers]: pop15less

log close
translate taxautonomy_revision_MMR.smcl taxautonomy_revision_MMR.log



*************************************************************************
log using taxautonomy_spatial.smcl
*************************************************************************
**SPATIAL WEIGHTED REGRESSIONS
*D:\Tax_autonomy\data\WEIGHT_15.dta<- this is WEIGHT_1 dropping Basque C. and Navarre.

*ssc install spregdpd

set matsize 1000

spregdpd primarydef taxrevenues currentoperexp after aftercurropexp growth employmentr, endog (taxrevenues currentoperexp aftercurropexp) pre(growth employmentr) nc(15) wmfile("D:\Tax_autonomy\data\WEIGHT_15.dta") model(sar) mfx(lin) run(xtabond) tests stand
estimates store column1
drop w1*

spregdpd primarydef currenttransfers currentoperexp after aftercurropexp growth employmentr, endog (currenttransfers currentoperexp aftercurropexp) pre(growth employmentr) nc(15) wmfile("D:\Tax_autonomy\data\WEIGHT_15.dta") model(sar) mfx(lin) run(xtabond) tests stand
estimates store column2
drop w1*

estimates table column1 column2, se
 
		*missing values in "agriculture" make the reg impossible without dropping years 1984 and 1985:
preserve //to temporarily drop some observations

	drop if year == 1984 | year == 1985
	spregdpd primarydef taxrevenues currentoperexp after aftercurropexp growth pop pop65up pop15less employmentr agriculture, endog (taxrevenues currentoperexp aftercurropexp) pre(growth employmentr) nc(15) wmfile("D:\Tax_autonomy\data\WEIGHT_15.dta") model(sar) mfx(lin) run(xtabond) tests stand
	estimates store column3
	drop w1*

	spregdpd primarydef currenttransfers currentoperexp after aftercurropexp growth pop pop65up pop15less employmentr agriculture, endog (currenttransfers currentoperexp aftercurropexp) pre(growth employmentr) nc(15) wmfile("D:\Tax_autonomy\data\WEIGHT_15.dta") model(sar) mfx(lin) run(xtabond) tests stand
	estimates store column4
	drop w1*

	estimates table column3 column4, se

restore //back to full dataset

estimates table column1 column2 column3 column4, se

log close
translate taxautonomy_spatial.smcl taxautonomy_spatial.log


*******************************************************************************
*******************************************************************************
log using taxautonomy_reverse, text replace
*******************************************************************************
		*SECOND REVISION*
		*MAIN REGRESSOR: DEBT PER CAPITA or Primary Deficit per capita
*******************************************************************************
quietly{
bys unit: gen L1debtcap=L1.debtcap
gen afterdebtcap=debtcap*after
bys unit: gen L1afterdebtcap=L1.afterdebtcap
bys unit: gen L1aftercurropexpcap=L1.aftercurropexpcap
bys unit: gen L1curropexpcap=L1.currentoperexpcap
 
gen histaxrevcap=histccaa*taxrevenuescap
gen histcurropexpcap=histccaa*currentoperexpcap
gen histcurrtranscap=histccaa*currenttransferscap
gen afterprimarydefcap=primarydefcap*after
bys unit: gen L1primarydefcap=L1.primarydefcap
bys unit: gen L1afterprimarydefcap=L1.afterprimarydefcap
 } 

************************************
	
	*DEPENDENT VARIABLE: CURRENT EXPENDITURE
		*NOTE: having the lagged variables as endog or pre does not change the coefficients or stdard errors AT ALL. It makes absolute no difference.

	*Model[1] lagged Debt per cap
xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth employmentr L1debtcap L1afterdebtcap) endog(currenttransferscap aftercurrtrancap) artests(2)
	 estat sargan
quietly{
xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth employmentr L1debtcap L1afterdebtcap) endog(currenttransferscap aftercurrtrancap) artests(2) vce(robust)
}
	 estat abond
	 
	 *Model[2]
xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth) endog(taxrevenuescap aftertaxrevcap L1debtcap L1afterdebtcap) artests(2)
	 estat sargan
quietly{
	 xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth) endog(taxrevenuescap aftertaxrevcap L1debtcap L1afterdebtcap) artests(2) vce(robust)
}
	 estat abond
	 

	*Model[3] lagged Deficit per cap
xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth employmentr) endog(currenttransferscap aftercurrtrancap L1primarydefcap L1afterprimarydefcap) artests(2)
	 estat sargan
quietly{
	 xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth employmentr) endog(currenttransferscap aftercurrtrancap L1primarydefcap L1afterprimarydefcap) artests(2) vce(robust)
}
	 estat abond
	 
	*Model[4]	 
xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth) endog(taxrevenuescap aftertaxrevcap L1primarydefcap L1afterprimarydefcap) artests(2)
	 estat sargan
quietly{
	 xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth) endog(taxrevenuescap aftertaxrevcap L1primarydefcap L1afterprimarydefcap) artests(2) vce(robust)
}
	 estat abond
	 
	*Model[5]
xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth employmentr) endog(L1primarydefcap L1afterprimarydefcap) artests(2)
	 estat sargan
quietly{
	 xtabond currentoperexpcap pop65up pop15less after, lags(1) pre (growth employmentr) endog(L1primarydefcap L1afterprimarydefcap) artests(2) vce(robust)
}
	 estat abond
	 
	*DEPENDENT VARIABLE: CURRENT TRANSFERS (income)

	*Model[6] regressor: current expenditure
xtabond currenttransferscap pop65up pop15less , lags(1) pre (growth employmentr) endog(currentoperexpcap aftercurropexpcap) artests(2)
	 estat sargan
quietly{
	 xtabond currenttransferscap pop65up pop15less , lags(1) pre (growth employmentr) endog(currentoperexpcap aftercurropexpcap) artests(2) vce(robust)
}
	 estat abond
	 
	*Model[7] regressor: lagged Deficit per cap
	 
xtabond currenttransferscap pop65up pop15less after, lags(1) pre (growth) endog(taxrevenuescap aftertaxrevcap L1primarydefcap L1afterprimarydefcap) artests(2)
	 estat sargan
quietly{
	 xtabond currenttransferscap pop65up pop15less after, lags(1) pre (growth) endog(taxrevenuescap aftertaxrevcap L1primarydefcap L1afterprimarydefcap) artests(2) vce(robust)
}
	 estat abond

*****Explore causality direction
* Dumitrescu-Hurlin test (p-value close to 0=rejection H0)
	*requires all variables are stationary, so:
	xtunitroot llc debtcap, demean /*debtcap has a unitroot*/
	xtunitroot llc currentoperexpcap, demean /*current exp has a unitroot*/
	xtunitroot llc primarydefcap,demean /*STATIONARY*/
	xtunitroot llc currenttransferscap,demean /*has a unitroot*/
	
bys unit: gen chdebtcap=debtcap-debtcap[_n-1]	 
bys unit: gen chcurrentoperexpcap=currentoperexpcap-L.currentoperexpcap
bys unit: gen chtransferscap=currenttransferscap-L.currenttransferscap


	 xtunitroot llc chdebtcap, demean /*demean option controls for cross-sectional dep. NOW, unit root is rejected at 1%*/
	 xtunitroot ips chdebtcap, demean /*H0:All panels contain unitroot is rejectec at 1%*/

	 xtunitroot llc chcurrentoperexpcap,demean /*unitroot rejected*/
	 xtunitroot llc chtransferscap, demean /*unitroot rejected*/

	 ssc install xtgcause
xtgcause chcurrentoperexpcap chdebtcap if year>1995, bootstrap lag(bic) /*H0 rejected=>debt does not cause current expendit*/
xtgcause chdebtcap chcurrentoperexpcap if year>1995, bootstrap lag(bic) /*H0 rejected=>current expendit change does not cause debt*/
xtgcause primarydefcap chtransferscap if year>1984, bootstrap lag(bic)
xtgcause chtransferscap primarydefcap  if year>1984, bootstrap lag(bic)

xtgcause chcurrentoperexpcap primarydefcap  if year>1984, bootstrap lag(bic)
    *se Wald statistics and p-values per ccaa:
	matrix Wi_PVi = r(Wi), r(PVi)
    matrix list Wi_PVi
xtgcause primarydefcap chcurrentoperexpcap if year>1984, bootstrap lag(bic)
    *se Wald statistics and p-values per ccaa:
	matrix Wi_PVi = r(Wi), r(PVi)
    matrix list Wi_PVi
	
log close
translate taxautonomy_reverse.smcl taxautonomy_reverse.log

**current expend cap has a unit root. Is it ok to have it as dep var wto doing anything else? (true we have a lag as regressor)
*regressions with these variables in variations provide exactly the same conclusions
gen afterchtransferscap=after*chtransferscap

***** Second Revision ****
* explore time fixed effects -putting off After and interaction terms
		*results don't make sense.
*create year dummy variables:
tabulate year, generate(yr)

xtabond currentoperexpcap pop65up pop15less yr2-yr36, lags(1) pre (growth employmentr) endog(currenttransferscap L1debtcap ) artests(2)
estat sargan
